﻿Imports System.Data

Partial Class Interfaz_frmSistemaConfiguracionMail
    Inherits System.Web.UI.Page
    Dim CL As New Logica.ModuloSistema.ControladorLogica
#Region "Datas"
    Dim dtDatosItems As New DataTable
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CL = Logica.ModuloSistema.ControladorLogica.getInstance


        If Not Page.IsPostBack Then
            CargarConfiguracionesAntiguasMail()
        End If
    End Sub


#Region "Funciones"
    Private Sub CargarDatosMailBDO(ByVal opcion As Integer)
        Try
            If opcion = 1 Then
                Dim smtp As String = Me.TxtSMTP.Text
                Dim nombreCuenta As String = Me.TxTNombreCuenta.Text
                Dim pass As String = Me.TxTPass.Text
                Dim repitePass As String = Me.TxTRepite.Text

                If (repitePass <> pass) Then
                    MsgBox("Las contraseñas no concuerdan intentelo nuevamente")
                Else
                    Dim nuevoMail As New Logica.ModuloSistema.Mail(smtp, nombreCuenta, pass, "A")
                    CL.insertaNuevoMail(nuevoMail)
                    clean()
                    Console.Write(" Los datos han sido almacenados correctamente")

                End If
            Else
                If opcion = 0 Then

                End If
            End If
        Catch ex As Exception
            Console.Write(ex.ToString)
        End Try
       
    End Sub

    Private Sub clean()
        Me.TxTNombreCuenta.Text = String.Empty
        Me.TxTPass.Text = String.Empty
        Me.TxTRepite.Text = String.Empty
        Me.TxtSMTP.Text = String.Empty
    End Sub

    Private Sub CargarConfiguracionesAntiguasMail()
        Try
            Dim arrayMail As ArrayList = CL.ObtenerMail()
            'Me.GridConfigMail.DataSource = Nothing
            'Me.GridConfigMail.DataBind()
            dtDatosItems.Columns.Clear()

            dtDatosItems.Columns.Add(New DataColumn("NUMERO"))
            dtDatosItems.Columns.Add(New DataColumn("SMTPHOST"))
            dtDatosItems.Columns.Add(New DataColumn("NOMBRECUENTA"))
            dtDatosItems.Columns.Add(New DataColumn("PASSWORD"))
            dtDatosItems.Columns.Add(New DataColumn("ESTADO"))


            Me.GridConfigMail.DataBind()
            Me.GridConfigMail.DataSource = Me.dtDatosItems


            If arrayMail.Count > 0 Then
                Me.filaTexto.Visible = True
                Me.BtnSetearCuentasExistente.Visible = True
            Else
                Me.filaTexto.Visible = False
                Me.BtnSetearCuentasExistente.Visible = False
            End If

            If arrayMail.Count > 0 Then
                Dim j As Integer

                For j = 0 To arrayMail.Count - 1

                    Dim drDatos As DataRow = dtDatosItems.NewRow
                    Dim mail As Logica.ModuloSistema.Mail = DirectCast(arrayMail.Item(j), Logica.ModuloSistema.Mail)

                    drDatos.Item("NUMERO") = mail.id
                    drDatos.Item("SMTPHOST") = mail.SmtpHost
                    drDatos.Item("NOMBRECUENTA") = mail.NomCuenta
                    drDatos.Item("PASSWORD") = mail.Password

                    If mail.Estado = "A" Then
                        drDatos.Item("ESTADO") = "Estado Actual"
                    Else
                        drDatos.Item("ESTADO") = "Estado No Actual"
                    End If

                    dtDatosItems.Rows.Add(drDatos)

                Next j
            End If

            Me.GridConfigMail.DataSource = dtDatosItems
            Me.GridConfigMail.DataBind()

        Catch ex As Exception
            Console.Write(ex.ToString)
        End Try
        
    End Sub

    Private Sub cambiarComfiguracionActual()
        Try
            Dim filaGrid As GridViewRow
            Dim contador As Integer = 0


            With Me.GridConfigMail
                For fila As Integer = 0 To .Rows.Count - 1
                    filaGrid = .Rows(fila)
                    Dim cb As CheckBox = filaGrid.FindControl("itemSelector")

                    If cb.Checked = True Then
                        contador = contador + 1
                    End If
                Next
            End With

            If contador = 1 Then
                With Me.GridConfigMail
                    For fila As Integer = 0 To .Rows.Count - 1
                        filaGrid = .Rows(fila)
                        Dim cb As CheckBox = filaGrid.FindControl("itemSelector")
                        Dim idMail As Integer = filaGrid.Cells(0).Text

                        If cb.Checked = True Then
                            CL.ResetMail(idMail, "A")
                        Else
                            CL.ResetMail(idMail, "NA")
                        End If
                    Next
                End With

            Else
                MsgBox("Debe seleccionar solo una configuracion")
            End If
        Catch ex As Exception
            Console.Write(ex.ToString)
        End Try
       
    End Sub
#End Region

#Region "Botones"
    Protected Sub BtnSetear1Cuenta_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSetear1Cuenta.Click
        CargarDatosMailBDO(1)
        CargarConfiguracionesAntiguasMail()
    End Sub

    Protected Sub BtnSetearCuentasExistente_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSetearCuentasExistente.Click
        cambiarComfiguracionActual()
        CargarConfiguracionesAntiguasMail()
    End Sub
#End Region

  
   
End Class
